package com.ymttest.database.mongo;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.http.impl.cookie.PublicSuffixFilter;
import org.json.JSONArray;

import com.mongodb.BasicDBList;
import com.mongodb.BasicDBObject;
import com.mongodb.MongoClientURI;
import com.mongodb.WriteResult;
import com.ymt.core.tool.Logger;
import com.ymt.database.MongoDBDriver;
import com.ymttest.business.service.AssertService;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.YMTDateUtil;

/************************************************************************************
 * 获取快照信息
 ************************************************************************************/
public class SnapShotWapper {
	
	MongoDBDriver mdd;
	public SnapShotWapper() throws Exception{
		mdd = new MongoDBDriver(new MongoClientURI(EnvSetup.getSystemUrl("set5_mongo_snapshot")));
		mdd.setPoolSize(Integer.valueOf(EnvSetup.getSystemUrl("mongoPoolSize")));
	}
	/**
	 * 查询版本信息 最多1000条
	 */
	public ArrayList<Map> getProductVersionInfo(String pid,Integer version) throws Exception {
		mdd.selectCollection("ProductVersionInfo");
		BasicDBObject search =new BasicDBObject();
		if(pid!=null){
			search.append("pid", pid);
		}
		if(version!=null){
			search.append("ver", version);
		}
		ArrayList<Map> list = mdd.selectPartLimitToMap(search,1000);
	    return list;
	}
	/**查询最近添加的记录 默认pid ver
	 * @param len
	 * @return
	 * @throws Exception
	 */
	public ArrayList<Map> getProductVersionInfoLens(int len,List<String> keys) throws Exception {
		mdd.selectCollection("ProductVersionInfo");
		ArrayList<Map> list=null;
		if(keys==null||keys.size()<=0){
		 list= mdd.selectPartLimitToMap("{}","{'_id':0,'pid':1,'ver':1}", len,"{'atime':-1}");
		}else{
			String query="";
			for (String key : keys) {
				query+="'"+key+"':1,";
			}
			query=query.substring(0, query.length()-1);
			 list= mdd.selectPartLimitToMap("{}","{'_id':0,"+query+"}", len,"{'atime':-1}");
		}
	    return list;
	}
	
	/**查询最近添加的记录 默认pid ver
	 * @param len
	 * @return
	 * @throws Exception
	 */
	public ArrayList<Map> getProductVersionInfoLens(String pid,int len,List<String> keys) throws Exception {
		mdd.selectCollection("ProductVersionInfo");
		ArrayList<Map> list=null;
		if(keys==null||keys.size()<=0){
		 list= mdd.selectPartLimitToMap("{'pid':'"+pid+"'}","{'_id':0,'pid':1,'ver':1}", len,"{'ver':-1}");
		}else{
			String query="";
			for (String key : keys) {
				query+="'"+key+"':1,";
			}
			query=query.substring(0, query.length()-1);
			 list= mdd.selectPartLimitToMap("{'pid':'"+pid+"'}","{'_id':0,"+query+"}", len,"{'ver':-1}");
		}
	    return list;
	}
	/**
	 * 更新sync
	 * @param pid
	 * @param version
	 * @param sync
	 * @return
	 * @throws Exception
	 */
	public Integer updateSyncProductVersionInfo(String pid,Integer version,Integer sync) throws Exception {
		mdd.selectCollection("ProductVersionInfo");
		BasicDBObject search =new BasicDBObject();
		if(pid!=null){
			search.append("pid", pid);
		}
		if(version!=null){
			search.append("ver", version);
		}
		BasicDBObject update =new BasicDBObject();
		update.append("$set", new BasicDBObject("sync", sync));
		WriteResult wr = mdd.update(search,update);
	    return wr.getN();
	}
	/**
	 * 查询规格
	 * @param pid
	 * @param ckey
	 * @return
	 * @throws Exception
	 */
	public ArrayList<Map> getProductCatalogInfo(String pid,String ckey) throws Exception {
		mdd.selectCollection("ProductCatalogInfo");
		BasicDBObject search =new BasicDBObject();
		if(pid!=null){
			search.append("pid", pid);
		}
		if(ckey!=null){
			search.append("ckey", ckey);
		}
		ArrayList<Map> list = mdd.selectPartLimitToMap(search,100);
	    return list;
	}
	/**
	 * 查询商品
	 * @param pid
	 * @param pkey
	 * @return
	 * @throws Exception
	 */
	public ArrayList<Map> getProductCoreInfo(String pid,String pkey) throws Exception {
		mdd.selectCollection("ProductCoreInfo");
		BasicDBObject search =new BasicDBObject();
		if(pid!=null){
			search.append("pid", pid);
		}
		if(pkey!=null){
			search.append("pkey", pkey);
		}
		ArrayList<Map> list = mdd.selectPartLimitToMap(search,100);
	    return list;
	}
	/**
	 * 查询图片
	 * @param pid
	 * @param pickey
	 * @return
	 * @throws Exception
	 */
	public ArrayList<Map> getProductPictureInfo(String pid,String pickey) throws Exception {
		mdd.selectCollection("ProductPictureInfo");
		BasicDBObject search =new BasicDBObject();
		if(pid!=null){
			search.append("pid", pid);
		}
		if(pickey!=null){
			search.append("pickey", pickey);
		}
		ArrayList<Map> list = mdd.selectPartLimitToMap(search,100);
	    return list;
	}
	public static void main(String[] args) throws Exception {
		SnapShotWapper ssw=new SnapShotWapper();
		String pid="62fb933b-f1ac-4105-8ba0-45b490d81a69";
		int ver=1001;
//		ssw.updateSyncProductVersionInfo(pid, ver, 0);
		ArrayList<Map> a= ssw.getProductVersionInfo(pid,ver);
		System.out.println(a);
		System.out.println(ssw.getProductCatalogInfo(pid, a.get(0).get("ckey").toString()));
		System.out.println(ssw.getProductCoreInfo(pid, a.get(0).get("pkey").toString()));
		System.out.println(ssw.getProductPictureInfo(pid, a.get(0).get("pickey").toString()));
		
//		List<String> keys=new ArrayList<>();
//		keys.add("pid");
//		keys.add("ver");
//		keys.add("sync");
//		System.out.println(ssw.getProductVersionInfoLens(100,keys));
		
	}   

}
